/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.anim;

import gjt.Orientation;

import java.awt.*;
import java.util.Enumeration;

/**
 * A CollisionDetector that detects collisions between Sprites
 * and the edges of the CollisionArena in which they reside.<p>
 *
 * @author Kristopher T Babic
 */
public class EdgeCollisionDetector extends CollisionDetector {
    public EdgeCollisionDetector(CollisionArena arena) {
        super(arena);
    }

    public void detectCollisions() {
        Enumeration sprites = arena.getSprites().elements();
        Dimension arenaSize = arena.getSize();
        Insets arenaInsets = arena.getInsets();
        AdvancedSprite sprite;

        while (sprites.hasMoreElements()) {
            sprite = (AdvancedSprite) sprites.nextElement();

            Point nl = sprite.getNextLocation();
// 1-30-00            Point mv     = sprite.getMoveVector();

// mods made on 2-15-00 to detect collisions with center of object
//			int   width  = sprite.getBounds().width;
//			int   height = sprite.getBounds().height;

            int nextRightEdge = nl.x;// + width;
            int nextBottomEdge = nl.y;// + height;
            int arenaBottomEdge = arenaSize.height -
                    arenaInsets.bottom;
            int arenaRightEdge = arenaSize.width -
                    arenaInsets.right;

            if (nextRightEdge > arenaRightEdge)
                arena.edgeCollision(sprite, Orientation.LEFT);
            else if (nl.x < arenaInsets.left)
                arena.edgeCollision(sprite, Orientation.RIGHT);

            if (nextBottomEdge > arenaBottomEdge)
                arena.edgeCollision(sprite, Orientation.BOTTOM);
            else if (nl.y < arenaInsets.top)
                arena.edgeCollision(sprite, Orientation.TOP);
        }
    }
}
